package ObjectVC;

import java.util.ArrayList;
import java.util.Date;
import javax.swing.JOptionPane;

public aspect Update {
	after(Object actualObj, Action action, ArrayList<Object> actionParams): 
		VCOperations.VCops(actualObj, action, actionParams) 
		&& if(action.equals(Action.Update)){
		if (null != actionParams && actionParams.size()>0){
			Object obj = actionParams.get(0);
			Integer idx = null;
			Date timestamp = null;
			Object newObj = null;
			
			if (null == obj){
				JOptionPane.showMessageDialog(null, "Object is null");
				return;
			}
			if(actionParams.size() != 3){
				JOptionPane.showMessageDialog(null, "Not enough parameters for update");
				return;
			}
			
			newObj = actionParams.get(2);
			if(actionParams.get(1).getClass().equals(Integer.class)) {
				idx = (Integer) actionParams.get(1);
				DB.getVersion(obj, idx).setObject(newObj);
			}
			else if(actionParams.get(1).getClass().equals(Date.class)) {
				timestamp = (Date) actionParams.get(1);
				DB.getVersion(obj, timestamp).setObject(newObj);
			}
		}
	}
}
